package org.unicat.serviceedu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.unicat.commonutils.ResultCode;
import org.unicat.servicebase.exceptionHandler.GuliException;
import org.unicat.serviceedu.entity.EduCourse;
import org.unicat.serviceedu.entity.EduCourseDescription;
import org.unicat.serviceedu.entity.vo.CourseAddVo;
import org.unicat.serviceedu.entity.vo.CoursePublishVo;
import org.unicat.serviceedu.entity.vo.CourseQuery;
import org.unicat.serviceedu.mapper.EduCourseMapper;
import org.unicat.serviceedu.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author unicat
 * @since 2022-02-22
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private EduChapterService chapterService;

    @Override
    public String addCourse(CourseAddVo courseVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseVo, eduCourse);

        if (!this.save(eduCourse)) {
            throw new GuliException(ResultCode.ERROR, "保存课程失败");
        }

        // 获取自动生成的课程id
        String cid = eduCourse.getId();

        // 简介表添加简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(cid);
        eduCourseDescription.setDescription(courseVo.getDescription());
        // 保存到简介表
        if (!eduCourseDescriptionService.save(eduCourseDescription)) {
            throw new GuliException(ResultCode.ERROR, "保存课程简介失败");
        }

        return cid;
    }

    @Override
    public CourseAddVo getCourseInfo(String courseId) {
        EduCourse courseInfo = this.getOne(new QueryWrapper<EduCourse>().eq("id", courseId));
        // 课程表id就是简介表id
        String description = eduCourseDescriptionService.getOne(new QueryWrapper<EduCourseDescription>().eq("id", courseId)).getDescription();

        //封装
        CourseAddVo courseAddVo = new CourseAddVo();
        BeanUtils.copyProperties(courseInfo, courseAddVo);
        courseAddVo.setDescription(description);

        return courseAddVo;
    }

    @Override
    public void updateCourseInfo(CourseAddVo courseVo) {
        // 找到记录
        EduCourse courseInfo = this.getOne(new QueryWrapper<EduCourse>().eq("id", courseVo.getId()));
        // 修改记录
        // 为null的数据忽略
        BeanUtil.copyProperties(courseVo, courseInfo, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));

        String cid = courseInfo.getId();
        EduCourseDescription courseDescription = eduCourseDescriptionService.getById(cid);
        courseDescription.setDescription(courseVo.getDescription());

        // 保存数据
        eduCourseDescriptionService.updateById(courseDescription);
        this.updateById(courseInfo);
    }

    @Override
    public CoursePublishVo getPublishCourseInfo(String courseId) {
        return baseMapper.getPublishCourseInfo(courseId);
    }

    @Override
    public void pageQuery(Page<EduCourse> pageParam, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");

        if (courseQuery == null){
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }

        String title = courseQuery.getTitle();
        String teacherId = courseQuery.getTeacherId();
        String subjectParentId = courseQuery.getSubjectParentId();
        String subjectId = courseQuery.getSubjectId();

        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }

        if (!StringUtils.isEmpty(teacherId) ) {
            queryWrapper.eq("teacher_id", teacherId);
        }

        if (!StringUtils.isEmpty(subjectParentId)) {
            queryWrapper.eq("subject_parent_id", subjectParentId);
        }

        if (!StringUtils.isEmpty(subjectId)) {
            queryWrapper.eq("subject_id", subjectId);
        }

        baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public boolean removeCourseById(String courseId) {
        //根据id删除所有视频
        videoService.removeByCourseId(courseId);

        //根据id删除所有章节
        chapterService.removeByCourseId(courseId);

        int result = baseMapper.deleteById(courseId);
        return result > 0;
    }
}
